<?php
require_once '../../server/utils/functions.php';
require_once '../../server/model/User.php';
if(isset($_SESSION["USER"]) && checkUserConnected($_SESSION["USER"])){
    
}else{
    echo("Caca0");
    exit(0);
}
$db = db();
$parameters = Array();
if(isset($_POST["MAC_ID"])&& !empty($_POST["MAC_ID"])){
    $parameters["MAC_ID"] = utf8_decode(htmlentities($_POST["MAC_ID"]));
}else{
    echo("Caca1");
    exit(0);
}    

if(isset($_POST["INC_TYPE"])&& !empty($_POST["INC_TYPE"])){
    $parameters["INC_TYPE"] = utf8_decode(htmlentities($_POST["INC_TYPE"]));
}else{
    echo("Caca3");
    exit(0);
}    
if(isset($_POST["INC_INFOS"])&& !empty($_POST["INC_INFOS"])){
    $parameters["INC_INFOS"] = utf8_decode(htmlentities($_POST["INC_INFOS"]));
}   
if(isset($_POST["INC_DATE"])&& !empty($_POST["INC_DATE"])){
    $parameters["INC_DATE"] = utf8_decode(htmlentities($_POST["INC_DATE"]));
}

$preReq = $db->prepare("SELECT MAX(INC_ID)+1 FROM INCIDENT WHERE MAC_ID=:MAC_ID");
$preReq->execute(Array("MAC_ID"=>$parameters["MAC_ID"]));
$preData = $preReq->fetch();
$preReq->closeCursor();
$parameters["INC_ID"] = $preData[0]==null?0:$preData[0];

$i = 0;
$request =  "";
$request2 = "";
foreach($parameters as $name=>$part){
    $request .= $name;
    if($name=="INC_DATE"){
        $request2 .= "TIMESTAMP(:".$name.")";
    }else{
        $request2 .= ":".$name;
    }
    $i++;
    if($i<sizeof($parameters)){
        $request .= ", ";
        $request2 .= ", ";
    }
}
$req = $db->prepare("INSERT INTO INCIDENT(".$request.") VALUES(".$request2.")");
if($req->execute($parameters)){
    echo("CacaSuccess");
}else{
    echo json_encode($req->errorInfo());
    echo("Caca4");
}
$req->closeCursor();
?>